December 29, 2019

0. Introduction

In this small project we will try out different plots that the plotly library has to offer. We will utilize plotly to produce:

  1. a simple line plot
  2. a histogram
  3. a boxplot
  4. a multidimensional 2D plot
library(plotly)

data sets being used:

data(presidents)
data(sunspot.year)
data(mtcars)

1.1 Line plot

A simple line plot of the quarterly approval ratings of US presidents.

plot_ly(data = as.data.frame(presidents), x = ~time(presidents),
  y = ~presidents, type = "scatter", mode = "lines") %>%
  layout(title = "Quarterly approval ratings over the years")

1.2 Histogram

A histogram of monthly sunspots numbers (1749-1983).

p <- plot_ly(data = as.data.frame(sunspot.year), x = ~sunspot.year,
  type = "histogram") %>%
  layout(title = "Sunspot occurrences histogram",
    yaxis = list(title = "count"))
p

1.3 Box plot

A boxplot visualizing the horsepower by cylinder relationship in the mtcars data set.

plot_ly(mtcars, x=~cyl, y = ~hp,
  color = ~as.factor(cyl), type = "box") %>%
  layout(title = "Box plot cyl~hp")

1.4 Multi dimensional plot

A 2D plot visualizing 4 dimensions of the mtcars data set (x-axis, y-axis, color and size of data point).

plot_ly(data = mtcars, x = ~wt, y = ~mpg, 
  color = ~as.factor(cyl), size = ~hp,
  type = "scatter", mode = "markers") %>%
  layout(title = "Car properties visualized")